import java.io.*;
import java.net.*;

abstract class Client extends Thread{

	/** client's socket.*/
	private Socket socket;
	/**data buffer srecived from socket.*/
    private BufferedReader in;
    /**data buffer sending to socket.*/
    private PrintWriter out;
    /**data received from client (first line input).*/
	protected boolean works;
	
	protected int Id;
	
	protected String nick;
	
	protected int[] temporaryHand;
	
	protected String shortName;
	
	private String response;
	
	protected boolean rejecting;
	/**
	* @param port server port
	* @param adres server adres
	* @param nick nick
	* @throws ClientException
	*
	*/
	public Client(int port, String adres, String nick) throws ClientException
	{
		response = " ";
		try{
            this.socket = new Socket(adres, port);
			this.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			this.out = new PrintWriter(socket.getOutputStream(), true);
		}
		catch(IOException e)
		{
			throw new ClientException("port or adres error");
		}
	    this.temporaryHand = new int[4];
		this.nick = nick;
		this.shortName = nick.trim();
		this.works = true;
		this.rejecting = false;
		out.println("Name_" + nick);
		
	}
	/**
	* @param number number
	* @return n converted string to int
	*/

	public int stringToInt(String number)
	{	int n = -1;
		try
		{
		 n = Integer.parseInt(number);
		}
		catch(NumberFormatException e)
		{
			System.err.println("Error with decode String for int");
		}
		return n;
		
	}
	public int getNumber()
	{
		return this.Id;
	}
	public void send(String response)
	{
		System.out.println("send" + response);
		out.println(response);
	}
	public void sendMessage(String response)
	{
		this.send("Chat_" + response);
	}
	public abstract void Chat(String msg);
	public abstract void bigBlind(String value);
	public abstract void smallBlind(String value);
	public abstract void sCoins(String value);
	public abstract void bet(String value);
	public abstract void enable(String buttons);
	public abstract void dAllin(String name);
	public abstract void stake(String stake);
	public abstract void round(String round);
	public abstract void cards(String cards);
	public abstract void Winner(String Winner);
	public abstract void Error(String msg);
	public abstract void Wait(String p);
	public abstract void ProposalToReject(String player);
	public abstract void PlayerRejecting(String player);
	public abstract void PlayerNotRejecting(String Player);
	public abstract void GameStart();
	public abstract void StartCoins(String coins);
	public abstract void StartCards(String cards);
	public abstract void PlayerDisconected(String player);
	public abstract void dFold(String player); 
	public abstract void ClientList(String list);
	public abstract void WaitForPlayers();
	public abstract void Stop();
	public abstract void out();
    public void sendBet(int bet){
        send("Bet_" + Integer.toString(bet));
}
public void sendStartGame(){
    send("StartGame_");
}
public void sendAddBot(){
    send("AddBot_");
}
public void sendDelete(int i){
	this.rejecting = true;
    send("RejctPlayer_" + i);
}
public void sendCheck(){
        send("Check_");
}
public void sendAllin(){
        send("All_");
}
public void sendFold(){
        send("Fold_");
}
public void sendExchange(String cardSet){
        send("Exchange_" + cardSet);
}
public void sendClose(){
        send("Close_");
}

	public void run()
	{System.out.println("i");
		String line="";
		while(works)
		{
			try{
				line = in.readLine();
                System.out.println("Line for "+ nick + ":" + line + "<");
            }
		
			catch(IOException e)
			{
				this.Error(e.getMessage());	
				System.out.println(e.getMessage());
			}
			if(line.startsWith("Number_"))
			{
				try{
					Id = Integer.parseInt(line.substring(7));
					System.out.println(Id);
				}
				catch(NumberFormatException e)
				{
					out.println("ERROR_Number");
				}
			}
			if(line.startsWith("Chat_"))
			{
				this.Chat(line.substring(5));
			}

			else if(line.startsWith("BigBlind_"))
			{

				this.bigBlind(line.substring(9));
			}
			else if(line.startsWith("SmallBlind_"))
			{
				this.smallBlind(line.substring(11));
			}
			else if(line.startsWith("sCoins_"))
			{
				this.sCoins(line.substring(7));
			}
			else if(line.startsWith("Bet_"))
			{
				this.bet(line.substring(4));
			}
			else if(line.startsWith("Enable_"))
			{
				this.enable(line.substring(7));
			}
			else if(line.startsWith("dFold_"))
			{
				this.dFold(line.substring(6));
			}
			else if(line.startsWith("dAllin_"))
			{
				this.dAllin(line.substring(7));
			}
			else if(line.startsWith("Stake_"))
			{
				this.stake(line.substring(6));
			}
			else if(line.startsWith("Round_"))
			{
				this.round(line.substring(6));
			}
			else if(line.startsWith("Cards_"))
			{
				this.cards(line.substring(6));
			}
			else if(line.startsWith("Winner_"))
			{
				
				this.Winner(line.substring(7));
			}
			else if(line.startsWith("ERROR_"))
			{
				this.Error(line.substring(6));
			}
			else if(line.startsWith("Wait_"))
			{
				this.Wait(line.substring(5));
			}
			else if(line.startsWith("WaitForPlayers_"))
			{
				this.WaitForPlayers();
			}
			else if(line.startsWith("proposalToReject_"))
			{
				this.ProposalToReject(line.substring(17));
			}
			else if(line.startsWith("PlayerRejecting_"))
			{
				this.PlayerRejecting(line.substring(16));
			}
			else if(line.startsWith("PlayerNotRejecting_"))
			{
				this.PlayerNotRejecting(line.substring(19));
			}
			else if(line.startsWith("GameStart_"))
			{
				this.GameStart();
			}
			else if(line.startsWith("startCoins_"))
			{
				this.StartCoins(line.substring(11));
			}
			else if(line.startsWith("startCards_"))
			{
				this.StartCards(line.substring(11));
			}
			else if(line.startsWith("startCards_"))
			{
				this.StartCards(line.substring(11));
			}
			else if(line.startsWith("PlayerDisconected_"))
			{
				this.PlayerDisconected(line.substring(18));
			}
			else if(line.startsWith("ClientList_"))
			{
				this.ClientList(line.substring(11));
			}
			else if(line.startsWith("Stop_"))
			{
				this.Stop();
			}
			else if(line.startsWith("OUT_"))
			{
				this.out();
			}
			if(response != " ")
			{
				out.println(response);
				response = " ";
			}
			try{
			this.sleep(5);
		}
		 catch (InterruptedException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		}
		
	}
	
	
	
}
